package com.qf.mapper;

import com.qf.model.Device;
import com.qf.model.DeviceType;
import com.qf.model.Farm;
import com.qf.model.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface DeviceMapper {
    @Results(id = "DeviceMap",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "sn",property = "sn"),
            @Result(column = "name",property = "name"),
            @Result(column = "location",property = "location"),
            @Result(column = "installTime",property = "installTime"),
            @Result(column = "manufacturer",property = "manufacturer"),
            @Result(column = "createtime",property = "createtime"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "userId",property = "userId"),
            @Result(column = "farmId",property = "farmId"),
            @Result(column = "typeId",property = "typeId"),
            @Result(column = "userId",property ="user",javaType = User.class,one=
            @One(select = "com.qf.mapper.UserMapper.findById" ,fetchType = FetchType.EAGER)
            ),
            @Result(column = "farmId", property = "farm", javaType = Farm.class,
                    one = @One(select = "com.qf.mapper.FarmMapper.findFarm",
                            fetchType = FetchType.EAGER)
            ),
            @Result(column = "typeId", property = "deviceType", javaType = DeviceType.class,
                    one = @One(select = "com.qf.mapper.DeviceTypeMapper.findDeviceType",
                            fetchType = FetchType.EAGER)
            )
    })
    @Select("select * from device")
    List<Device> getAll();

    @ResultMap("DeviceMap")
    @Select("select * from device limit #{currentPage}, #{pageSize}")
    List<Device> findPage(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize);
    @Select("select count(*) from device")
    long count();

    @Insert("insert into device (sn,name,location,installTime,manufacturer,createtime,updatetime,farmId,typeId) values" +
            " (#{sn},#{name},#{location},#{installTime},#{manufacturer},#{createtime},#{updatetime},#{farmId},#{typeId})")
    int insertDevice(Device device);
    @ResultMap("DeviceMap")
    @Select("select * from device where id =#{id}")
    Device findDevice(int id);

    @Update("update device set sn=#{sn},name=#{name},location=#{location},installTime=#{installTime},manufacturer=#{manufacturer}" +
            ",createtime=#{createtime},updatetime=#{updatetime},farmId=#{farmId},typeId=#{typeId} where id=#{id}")
    int update(Device device);
    @Delete("delete from device where id =#{id}")
    int delDevice(Integer id);
}
